
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Namespace;

public class AddToCartServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
	Namespace ns = Namespace.getNamespace("http://www.cs.au.dk/dWebTek/2011");

	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,
	IOException {
		String itemName = req.getParameter("itemName");
		String amount = req.getParameter("amount");
		String itemID = req.getParameter("itemID");
		
		
		res.setContentType("text/html");
		HttpSession session = req.getSession();
		session.setMaxInactiveInterval(200);

		//if no purchase has been made, create new list
		List<String> products = 	(List<String>) session.getAttribute("products");
		List<String> amountList = 	(List<String>) session.getAttribute("amount");
		List<String> itemIDs = 		(List<String>) session.getAttribute("itemIDs");
		if(products == null){
			products = new ArrayList<String>();
			amountList = new ArrayList<String>();
			itemIDs = new ArrayList<String>();
		}
		
		products.add(itemName);
		amountList.add(amount);
		itemIDs.add(itemID);
		
		for(int i = 0; i < products.size(); i++){
			System.out.print(products.get(i) + " ");
			System.out.print(amountList.get(i) + " ");
			System.out.println(itemIDs.get(i));
		}
		
		
		session.setAttribute("products", products);
		session.setAttribute("amount", amountList);
		session.setAttribute("itemIDs", itemIDs);

		// Redirecting the client to the frontpage
		res.sendRedirect("index.jsp");
		return;
	}
}
